﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Level26EnglishLettersAdd : MonoBehaviour
{
    public List<TextButton> tbList = new List<TextButton>();
    private TextButton curActiveButton = null;
    private TextButton addToButton = null;

    public Color normalColor;
    public Color activeColor;
    public GameLevelItem gameLevelItem;

    // Start is called before the first frame update
    void Start()
    {
        InitTBList();
    }

    private void InitTBList()
    {
        int count = tbList.Count;
        for (int i = 0; i < count; i++)
        {
            int tempIndex = i;
            tbList[i].Init("a", () =>
            {
                OnTextButtonClick(tempIndex);
            });
        }
    }

    private void OnTextButtonClick(int tbIndex)
    {
        if (curActiveButton == null)
        {
            curActiveButton = tbList[tbIndex];
            curActiveButton.button.image.color = activeColor;
        }
        else
        {
            addToButton = tbList[tbIndex];
            if(addToButton == curActiveButton)
            {
                curActiveButton.button.image.color = normalColor;
                curActiveButton = null;
                return;
            }
            int curActiveLetterNumber = GameCtrl.Instance.Letter2Number(curActiveButton.text.text);
            int addToLetterNumber = GameCtrl.Instance.Letter2Number(addToButton.text.text);
            addToLetterNumber = addToLetterNumber + curActiveLetterNumber;
            if (addToLetterNumber > 26)
                addToLetterNumber = addToLetterNumber - 26;
            addToButton.text.text = GameCtrl.Instance.Number2Letter(addToLetterNumber);
            curActiveButton.button.image.color = normalColor;
            curActiveButton = null;
            JudgeResult();
        }
    }


    private void JudgeResult()
    {
        string resultStr = string.Empty;
        int count = tbList.Count;
        for (int i = 0; i < count; i++)
        {
            resultStr += tbList[i].text.text;
        }

        if (resultStr.Equals(UConst.Str_TargetLove))
            gameLevelItem.TriggerResult(true);


    }


}
